import java.sql.SQLOutput;

public class MySingList {
    class Node {
        public int val;
        public Node next;
        public Node(int val) {
            this.val = val;
        }


    }
    public Node head;
    //测试用例
    public void creatList() {
        Node node1 = new Node(10);
        Node node2 = new Node(11);
        Node node3 = new Node(12);
        Node node4 = new Node(13);
        Node node5 = new Node(14);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = null;
        head = node1;
    }
    //链表清除
    public void clear() {
        head = null;
    }
    //删除所有出现关键字的节点
    public void removeAllKey(int key) {
        Node fakeHead = new Node(0);
        fakeHead = head;
        Node temp = fakeHead;
        while(temp.next != null) {
            if(temp.next.val == key) {
                temp.next = temp.next.next;
            }else {
                temp = temp.next;
            }
        }
        head = fakeHead.next;
    }
    //删除第一次出现的关键字
    public void remove(int key) {
        if(head.val == key) {
            head = head.next;
            return;
        }
        Node cur = searchPreIndex(key);
        if(cur == null) {
            return ;
        }
        cur.next = cur.next.next;
    }
    //找到要删除的节点前一个位置
    private Node searchPreIndex(int key) {
        if(head == null) {
            return null;
        }
        Node cur = head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //指定节点插入
    public void addIndex(int index,int date) throws MyListIndexOutExcption{
        checkIndex(index);
        if(index == 0) {
            addFirst(date);
        }
        if(index == size()) {
            addLast(date);
        }
        Node node = new Node(date);
        Node cur = findPreIndex(index);
        node.next = cur.next;
        cur.next = node;
    }
    //找到要插入的下标的前一个位置
    public Node findPreIndex(int index) {
        Node cur = head;
        int count = 0;
        while(count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //检查下标是否合法
    private void checkIndex(int index) throws MyListIndexOutExcption{
        if(index < 0 || index > size()) {
            throw new MyListIndexOutExcption("位置不合法");
        }
    }
    //尾插法
    public void addLast(int date) {
        Node node = new Node(date);
        if(head == null) {
            head = node;
        }else {
            Node cur = head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }
    //头插法
    public void addFirst(int date) {
        Node node = new Node(date);
        node.next = head;
        head = node;
    }
    //显示
    public void disPlay() {
        Node cur = head;
        while(cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }
    //是否包含
    public boolean contains(int val) {
        Node cur = head;
        while(cur != null) {
            if(cur.val == val) {
                return true;
            }else {
                cur = cur.next;
            }
        }
        return false;
    }
    //链表长度
    public int size() {
        Node cur = head;
        int count = 0;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }
}
